package com.njtech.pet.controller;

import com.alibaba.fastjson.JSON;
import com.njtech.pet.constant.Constant;
import com.njtech.pet.entity.Pet;
import com.njtech.pet.entity.User;
import com.njtech.pet.service.PetService;
import com.njtech.pet.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.*;

/**
 * Author:gm
 * Date:2020-03-14 17:39
 * Description:<描述>
 */
@Controller
@RequestMapping("/pet")
public class PetController {
    @Autowired
    PetService petService;
    @Autowired
    UserService userService;

    /**
     * 新增宠物
     * @return
     */
    @RequestMapping(value={"/insertPet"})
    @ResponseBody
    public String insertPet(@RequestParam String name, @RequestParam String species, @RequestParam String birthday, @RequestParam String lisence, @RequestParam String immune, @RequestParam String color, @RequestParam String sex, @RequestParam String birthControl, @RequestParam String chip, @RequestParam String remark, @SessionAttribute(value = "user") User user){
        Map<String,Object> resultMap = new HashMap<>();
        Pet pet = new Pet();
        pet.setName(name);
        pet.setSpecies(species);
        Date date = parsingDate(birthday);
        pet.setBirthday(date);
        pet.setLisence(lisence);
        pet.setImmune(immune);
        pet.setSex(sex);
        pet.setColor(color);
        pet.setBirthControl(birthControl);
        pet.setChip(chip);
        pet.setUser(user);
        pet.setState(Constant.Pet.STATE_VALID.getValue());
        pet.setRemark(remark);

        int cnt = petService.insertPet(pet);
        if(cnt==1) {
            resultMap.put("msg","insetPet成功！" );
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     *  功能2：删除宠物
     * @param id    宠物id
     * @return      提示信息
     */
    @RequestMapping(value={"/deletePet"})
    @ResponseBody
    public String deletePet(Integer id){
        if(id==null){
            return null;
        }
        Map<String,Object> resultMap = new HashMap<>();
        //找到pet
        Pet pet = petService.selectByPetId(id);
        pet.setState(Constant.Pet.STATE_INVALID.getValue());
        int flag = petService.deletePet(pet);
        if(flag==1){
            resultMap.put("msg","pet删除成功！");
        }else{
            resultMap.put("msg","pet删除失败！");
        }
        return JSON.toJSONString(resultMap);
    }


    /**
     *功能3：修改宠物
     * @return  提示信息
     */
    @RequestMapping(value={"/updatePet"})
    @ResponseBody
    public String updatePet(@RequestParam Integer id,@RequestParam String name, @RequestParam String species,@RequestParam String birthday,@RequestParam String lisence,@RequestParam String immune,@RequestParam String color, @RequestParam String sex,@RequestParam String birthControl,@RequestParam String chip,@RequestParam String remark){
        Map<String,Object> resultMap = new HashMap<>();
        //找到pet
        Pet pet = petService.selectByPetId(id);
        pet.setId(id);
        pet.setName(name);
        pet.setSpecies(species);
        Date date = parsingDate(birthday);
        pet.setBirthday(date);
        pet.setLisence(lisence);
        pet.setImmune(immune);
        pet.setColor(color);
        pet.setSex(sex);
        pet.setBirthControl(birthControl);
        pet.setChip(chip);
        pet.setRemark(remark);
        int flag=petService.updatePetMessage(pet);
        System.out.println(flag);
        if(flag==1){
            resultMap.put("msg","pet修改成功！");
        }else{
            resultMap.put("msg","pet修改失败！");
        }
        return JSON.toJSONString(resultMap);
    }

    /**
     * 功能4：查询所有宠物
     * @return 结果集
     */
    @RequestMapping(value={"/selectPets"})
    @ResponseBody
    public String selectByUserId(@SessionAttribute(value = "user") User user){
        if(user==null){
            return null;
        }
        List<Pet> pets =  petService.selectByUserId(user);
        return JSON.toJSONString(pets);
    }

    /**
     * 功能4：查询一条宠物
     * @param id  宠物id
     * @return 结果集
     */
    @RequestMapping(value={"/selectPet"})
    @ResponseBody
    public String selectPet(@RequestParam Integer id){
        if(id==null){
            return null;
        }
        Pet pet = petService.selectByPetId(id);
        return JSON.toJSONString(pet);
    }


    /**
     * 用于日期格式转化
     * @param date  字符串类型
     * @return      Date类型：2018-08-09 能放入数据库
     */
    public Date parsingDate(String date){
        String[] str =date.split("-");
        List<String> list = new ArrayList<>();
        list.add(str[0]);
        list.add(str[1]);
        list.add(str[2]);
        Calendar c1 = Calendar.getInstance();
        c1.set(Integer.parseInt(list.get(0)), Integer.parseInt(list.get(1)),Integer.parseInt(list.get(2)) );
        System.out.println(c1.getTime());
        return c1.getTime();
    }
}
